package com.wtp.wallpaper.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtp.wallpaper.pojo.BaseEntity;
import com.wtp.wallpaper.pojo.vo.PictureVO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class PojoConvertUtil {

    public static <PO,VO> List<VO> convert(List<PO> list, Class<VO> voClass){
     /*   BeanUtil.copyToList(list,voClass,
                CopyOptions.create().setFieldValueEditor((field,value) -> {
                    if(field.equals("tabs") && value instanceof String){
                        return ((String) value).split(",");
                    }
                    return value;
                }));*/
        return BeanUtil.copyToList(list,voClass);
    }

    public static <PO,VO> List<VO> convert(List<PO> list, Function<PO,VO> convert){
        return list.stream().map(convert).collect(Collectors.toList());
    }

    public static <PO,VO> Page<VO> convert(Page<PO> page, Class<VO> voClass){
        Page<VO> result = new Page<>();

        BeanUtil.copyProperties(page,result,CopyOptions.create().setFieldValueEditor((field,value) -> {
            if(field.equals("records") && value instanceof List){
                return BeanUtil.copyToList((Collection<?>) value,voClass);
            }
            return value;
        }));

        return result;
    }
}
